<!--
 * @Description: 三方检测报告
 * @Author: wangwangwang
 * @Date: 2020-09-21 14:09:06
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-12-29 14:56:21
-->
<template>
  <div class="pages_content table">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main style="display:flex">
        <div>
          <div class="tabtop">
            <div v-for="(item, index) in arr1" :key="index" @click="choose(item.id)">
              <div :class="active1 == index ? 'active' : 'text'">{{ item.value }}</div>
            </div>
          </div>
          <div class="header_right" @click="showCardNav = !showCardNav">
            <div class="title">搜索</div>
            <div>
              <transition name="from"></transition>
              <span v-show="!showCardNav">
                <i style="margin-right: 5px;" class="el-icon-arrow-down"></i>
              </span>
              <transition name="to"></transition>
              <span v-show="showCardNav">
                <i style="margin-right: 5px;" class="el-icon-arrow-up"></i>
              </span>
            </div>
          </div>
          <div v-show="showCardNav" class="hr"></div>
          <el-collapse-transition>
            <div v-show="showCardNav">
              <div class="search">
                <span style="display: inline-block;margin-bottom: 10px;">
                  <span style="margin-right: 12px;">校验时间</span>
                  <el-date-picker v-model="value1" size="medium" type="datetimerange" unlink-panels start-placeholder="开始时间" end-placeholder="结束时间" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
                </span>
                <span v-if="active1==0" style="margin-left: 24px;">
                  <span style="margin-right: 12px;">原物料名称</span>
                  <el-input v-model="tableParam.searchName" style="width: 240px;" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
                </span>
                <span v-else style="margin-left: 24px;">
                  <span style="margin-right: 12px;">产品名</span>
                  <el-input v-model="tableParam.searchName" style="width: 240px;" clearable placeholder="请输入" />
                </span>
                <el-button icon="el-icon-search" size="medium" class="bottom_all" @click="(tableParam.page = 1), gettableData()">查 询</el-button>
                <el-button size="medium" class="bottom_all_two" style="margin-left:12px;" @click="init()">重 置</el-button>
              </div>
            </div>
          </el-collapse-transition>
          <div class="hr"></div>
          <div class="card">
            <div class="card_nav">
              <div>
                <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='TIANJIA'))!==-1" size="small" class="bottom_all" style="margin-left: 0;" @click="rest(),addFromDate('添加')">
                  <i class="icon iconfont iconicon_button_add" style="font-size: 12px"></i>
                  添加
                </el-button>
              </div>
              <div>
                <el-dropdown :hide-on-click="false" trigger="click" style="margin: 0 20px;">
                  <el-link :underline="false">
                    <span class="el-icon--left">
                      <i class="iconfont iconicon_button_filtrate"></i>
                    </span>
                    筛选
                  </el-link>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item v-for="(item, index) in table" :key="index">
                      <el-checkbox v-model="item.status">{{ item.label }}</el-checkbox>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
                <!-- <el-link :underline="false" @click="load()">
                  <span class="el-icon--left">
                    <i class="iconfont iconicon_button_export1"></i>
                  </span>
                  导出
                </el-link> -->
              </div>
            </div>
            <div v-loading="tableLoading" class="card_info" :style="style">
              <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableData" tooltip-effect="dark">
                <template v-for="(item, index2) in table">
                  <el-table-column v-if="item.status" :key="index2" :label="item.label" align="center" :min-width="item.width" :fixed="(item.label =='操作'||item.label =='检验报告')?'right':false">
                    <template slot-scope="scope">
                      <span v-if=" item.label == '检验报告'">
                        <el-button class="bottom_one" size="mini" @click="open('Dialog2', scope.row)">查看</el-button>
                      </span>
                      <span v-else-if=" item.label == '操作'">
                        <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='XIUGAI'))!==-1" class="bottom_one" size="mini" @click="open('addDialog', scope.row)">修改</el-button>
                      </span>
                      <span v-else>{{ scope.row[item.prop] }}</span>
                    </template>
                  </el-table-column>
                </template>
              </el-table>
            </div>
          </div>
        </div>
      </el-main>
      <div class="card_bottom">
        <div class="card_bottom_left">
          <div class="top_page_totle">总共{{ total }}条</div>
          <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" :page-sizes="[10, 20, 30, 40,50,100,200,500,1000]" layout="prev,pager,sizes,next,jumper" :total="total" @size-change="sizeChange" @current-change="gettableData()"></el-pagination>
        </div>
        <div>
          <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
        </div>
      </div>
    </el-container>
    <!-- 查看 -->
    <el-dialog v-dialogDrag :visible.sync="Dialog2" width="880px" :close-on-click-modal="false">
      <span slot="title" class="dialog-title">查看</span>
      <div>
        <div class="dialoginfo-title">
          <div>
            <span>校验时间：</span>
            <span>{{low.inspectionTime}}</span>
          </div>
          <div>
            <span>检验单位：</span>
            <span>{{low.inspectionUnit}}</span>
          </div>
          <div>
            <span>检验结果：</span>
            <span>{{low.inspectionResult}}</span>
          </div>
        </div>
        <div class="download">
          <div class="tl">检验报告：</div>
          <div class="items">
            <template v-for="(item,index) in listFilesa">
              <span :key="index" class="loadItem" @click="$wps(item.path, 1)"><i class="el-icon-document"></i>{{item.name}}</span>
            </template>
          </div>

        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="Dialog2=false">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 添加 -->
    <el-dialog v-dialogDrag :visible.sync="addDialog" width="760px" :close-on-click-modal="false" :before-close="resetFromDate">
      <span slot="title" class="dialog-title">{{ addDialogTitle }}</span>
      <div>
        <el-form ref="reffromDate" :rules="rules" :inline="true" :model="fromDate" label-width="110px">
          <el-form-item prop="reportNo">
            <span slot="label" style="color: #ff0000">报告编号</span>
            <el-input v-model="fromDate.reportNo" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item v-if="active1==0" prop="supplierName">
            <span slot="label">供应商</span>
            <el-input v-model="fromDate.supplierName" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item v-if="active1==0" prop="rawId">
            <span slot="label" style="color: #ff0000">原物料名称</span>
            <el-select v-model="fromDate.rawId" placeholder="请选择">
              <el-option v-for="(item, index) in arr5" :key="index" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="active1==1" prop="engineeringName">
            <span slot="label">工程名称</span>
            <el-input v-model="fromDate.engineeringName" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item v-if="active1==1" prop="pouringPart">
            <span slot="label">浇筑部位</span>
            <el-input v-model="fromDate.pouringPart" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <!-- 生成产品名 -->
          <el-form-item v-if="active1==1">
            <span slot="label" style="color: #ff0000">强度等级</span>
            <el-select v-model="name1" placeholder="请选择" @change="change">
              <el-option v-for="(item, index) in typeList1.list" :key="index" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="active1==1">
            <span slot="label">规范性能等级</span>
            <el-select v-model="name2" placeholder="请选择" multiple @change="change">
              <el-option v-for="(item, index) in typeList2.list" :key="index" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="active1==1">
            <span slot="label">材料要求</span>
            <el-select v-model="name3" placeholder="请选择" multiple @change="change">
              <el-option v-for="(item, index) in typeList3.list" :key="index" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="active1==1">
            <span slot="label">特殊要求</span>
            <el-select v-model="name4" placeholder="请选择" multiple @change="change">
              <el-option v-for="(item, index) in typeList4.list" :key="index" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="active1==1" prop="engineeringName">
            <span slot="label">其他要求</span>
            <el-select v-model="name5" placeholder="请选择" multiple @change="change">
              <el-option v-for="(item, index) in typeList6.list" :key="index" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <div v-if="active1==1">
            <el-form-item prop="productName">
              <span slot="label" style="color: #ff0000">产品名</span>
              <el-input v-model="fromDate.productName" disabled style="width:500px"></el-input>
            </el-form-item>
          </div>
          <el-form-item prop="inspectionTime">
            <span slot="label">校验时间</span>
            <el-date-picker v-model=" fromDate.inspectionTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item prop="inspectionUnit">
            <span slot="label">校验单位</span>
            <el-input v-model="fromDate.inspectionUnit" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item prop="inspectionResult">
            <span slot="label">校验结果</span>
            <el-select v-model="fromDate.inspectionResult" placeholder="请选择">
              <el-option value="OK"></el-option>
              <el-option value="NG"></el-option>
            </el-select>
          </el-form-item>
          <div>
            <el-form-item prop="companyId" :rules="[
                    {
                      required: true,
                      message: '请选择公司',
                      trigger: 'blur',
                    },
                  ]">
              <span slot="label">公司</span>
              <el-select ref="selection1" v-model="fromDate.companyId" placeholder="请选择" @change="$nextTick(()=>{fromDate.company = $refs.selection1.selectedLabel})">
                <el-option v-for="item in companylist" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </div>
          <el-form-item>
            <span slot="label">校验报告</span>
            <FileComponent fileCode="testreportFile" :files="listFilesa" @deleteFile="deleteFile" @uplodeSuccess="uplodeSuccess" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resetFromDate">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px" @click="pagessubmitFromDate()">{{fromDate.inspectionResult==='NG'?'提交审核':'确定'}}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import mixin from "@/mixins/ICPSmixin.js";
import FileComponent from '@/components/File/fileComponent';
import {
  queryPage1,
  saveOrUpdate1,
  queryPage2,
  saveOrUpdate2,
  get_report_list
} from "@/api/smart/soiltest/c.js";
import { getLists } from "@/api/basicConfig/CCTrules.js";
import { getDicType } from "@/api/common.js";
import { permissionsCompanyList } from "@/api/common.js";
export default {
  components: { FileComponent },
  mixins: [mixin],
  data() {
    return {
      companylist: [],
      /*表格增删改查及下载接口*/
      queryPage: queryPage1,
      saveOrUpdate: saveOrUpdate1,
      /*表格数据*/
      table: [],
      table1: [
        { label: "报告编号", width: "auto", prop: "reportNo", status: true },
        { label: "供应商", width: "120", prop: "supplierName", status: true },
        { label: "原物料名称", width: "120", prop: "rawName", status: true },
        {
          label: "检验时间",
          width: "160",
          prop: "inspectionTime",
          status: true,
        },
        {
          label: "检验结果",
          width: "auto",
          prop: "inspectionResult",
          status: true,
        },
        {
          label: "检验单位",
          width: "auto",
          prop: "inspectionUnit",
          status: true,
        },
        { label: "检验报告", width: "120", prop: "", status: true },
        { label: "操作", width: "120", prop: "", status: true },
      ],
      table2: [
        { label: "ID", width: "160", prop: "reportNo", status: true },
        {
          label: "工程名称",
          width: "160",
          prop: "engineeringName",
          status: true,
        },
        { label: "浇筑部位", width: "120", prop: "pouringPart", status: true },
        { label: "产品名", width: "320", prop: "productName", status: true },
        {
          label: "检验时间",
          width: "160",
          prop: "inspectionTime",
          status: true,
        },
        {
          label: "检验结果",
          width: "120",
          prop: "inspectionResult",
          status: true,
        },
        {
          label: "检验单位",
          width: "140",
          prop: "inspectionUnit",
          status: true,
        },
        { label: "检验报告", width: "100", prop: "", status: true },
        { label: "操作", width: "100", prop: "", status: true },
      ],
      pagesType: 0,
      /*添加表单数据*/
      arr1: [
        { value: "原物料检验", id: 0 },
        { value: "混凝土检验", id: 1 },
      ],
      active1: 0,
      low: "",
      value1: [],
      /*添加*/
      rules: {
        reportNo: [
          { required: true, message: "请输入报告编号", trigger: "blur" },
        ],
        rawId: [{ required: true, message: "请选择原物料", trigger: "change" }],
        productName: [
          { required: true, message: "请选择强度等级", trigger: "blur" },
        ],
      },
      fromDate: {
        fileList: [],
        strengthGrade: [],
        performanceGrade: "性能等级",
        meterialRequirements: "材料要求",
        specialRequirements: "特殊要求",
        otherRequirements: "其他要求",
      },
      arr2: [],
      name1: "",
      name2: [],
      name3: [],
      name4: [],
      name5: [],
      listFilesa: [],
      oldlistFilesa: [],
      arr5: [],
      Dialog2: false,
      typeList1: [],
      typeList2: [],
      typeList3: [],
      typeList4: [],
      typeList5: [],
      typeList6: [],
    };
  },
  async created() {
    this.companylist = await permissionsCompanyList()
    /*存储拷贝的原初始化fromDate数据*/
    getDicType({ code: "MATname" }).then((res) => {
      this.arr5 = res.data.items;
    });
    getLists().then((res) => {
      this.arr2 = res.data;
      this.typeList1 = res.data.find((res) => res.typeId == 1) || [];
      this.typeList2 = res.data.find((res) => res.typeId == 2) || [];
      this.typeList3 = res.data.find((res) => res.typeId == 3) || [];
      this.typeList4 = res.data.find((res) => res.typeId == 4) || [];
      this.typeList5 = res.data.find((res) => res.typeId == 5) || [];
      this.typeList6 = res.data.find((res) => res.typeId == 6) || [];
    });
  },
  watch: {
    active1: {
      handler() {
        this.$data.tableParam = {
          ...this.$options.data().tableParam,
          startDate: "",
          endDate: "",
          searchName: "",
        };
        switch (this.active1) {
          case 0: {
            this.table = this.table1;
            this.queryPage = queryPage1;
            this.saveOrUpdate = saveOrUpdate1;
            this.gettableData();
            return;
          }
          case 1: {
            this.table = this.table2;
            this.queryPage = queryPage2;
            this.saveOrUpdate = saveOrUpdate2;
            this.gettableData();
            return;
          }
        }
      },
      deep: false, //是否深度监听，true为是，默认为false
      immediate: true, //immediate表示在watch中首次绑定的时候，是否执
    },
    value1() {
      if (!this.value1) {
        this.tableParam.startDate = ''
        this.tableParam.endDate = ''
      }
      this.tableParam.startDate = this.value1[0];
      this.tableParam.endDate = this.value1[1];
    },
  },
  methods: {
    /**
     * @Description: 删除附件
     * @Param: 
     * @Author: wangwangwang
     */
    deleteFile(index) {
      this.listFilesa.splice(index, 1);
    },
    /**
     * @Description: 上传成功
     * @Param: 
     * @Author: wangwangwang
     */
    uplodeSuccess(data) {
      let item = {
        name: data.fileName,
        fileName: data.fileName,
        path: data.fileUrl,
      };
      this.listFilesa.push(item);
    },
    change() {
      let _this = this
      _this.fromDate.strengthGrade = _this.arr2.find((res) => res.typeId == 1)?.list.filter(
        item => (item.id == _this.name1)
      )
      _this.fromDate.performanceGrade = _this.arr2.find((res) => res.typeId == 2)?.list.filter(
        item => (_this.name2.some((items) => (item.id == items)))
      )
      _this.fromDate.meterialRequirements = _this.arr2.find((res) => res.typeId == 3)?.list.filter(
        item => (_this.name3.some((items) => (item.id == items)))
      )
      _this.fromDate.specialRequirements = _this.arr2.find((res) => res.typeId == 4)?.list.filter(
        item => (_this.name4.some((items) => (item.id == items)))
      )
      _this.fromDate.otherRequirements = _this.arr2.find((res) => res.typeId == 6)?.list.filter(
        item => (_this.name5.some((items) => (item.id == items)))
      )
      let text2 = _this.fromDate.performanceGrade.map(res => (res.name))
      let text3 = _this.fromDate.meterialRequirements.map(res => (res.name))
      let text4 = _this.fromDate.specialRequirements.map(res => (res.name))
      let text5 = _this.fromDate.otherRequirements.map(res => (res.name))
      let arr = [...text2, ...text3, ...text4, ...text5];
      let text1 = _this.fromDate.strengthGrade[0] ?.name
      _this.fromDate.betonId = _this.fromDate.strengthGrade[0] ?.id
      arr.unshift(text1);
      _this.fromDate.productName = arr.join("");
    },
    rest() {
      this.name1 = "";
      this.name2 = [];
      this.name3 = [];
      this.name4 = [];
      this.name5 = [];
      this.listFilesa = [];
    },
    init() {
      /*重置数据*/
      this.tableParam.searchName = "";
      this.value1 = "";
      this.gettableData();
    },
    choose(num) {
      this.active1 = num;
    },
    async open(key, low) {
      let _this = this
      await get_report_list({ reportId: low.id }).then(res => {
        this.listFilesa = res.data.map(res => {
          let obj = {
            name: res.fileName,
            fileName: res.fileName,
            path: res.filePath,
            dataId: res.id
          }
          return obj
        })
        this.oldlistFilesa = this.copy(this.listFilesa)
      })
      if (key == "addDialog") {
        _this.addDialogTitle = "修改";
        _this[key] = true;
        _this.fromDate = _this.copy(low);
        _this.fromDate.fileList = new Array();
        if (_this.active1 == 1) {
          _this.name1 = low.betonId
          _this.name2 = JSON.parse(low.performanceGrade).map(res => (res.id))
          _this.name3 = JSON.parse(low.meterialRequirements).map(res => (res.id))
          _this.name4 = JSON.parse(low.specialRequirements).map(res => (res.id))
          _this.name5 = JSON.parse(low.otherRequirements).map(res => (res.id))
        }

      } else {
        _this[key] = true;
        _this.low = low;
      }
    },
    pagessubmitFromDate() {
      let _this = this;
      _this.$refs.reffromDate.validate((valid) => {
        if (valid) {
          if (this.active1 == 0) {
            _this.fromDate.rawName = _this.$fromatTable(
              _this.arr5,
              _this.fromDate.rawId,
              "id",
              "itemName"
            );
          }
          if (this.active1 == 1) {
            _this.fromDate.strengthGrade = typeof (_this.fromDate.strengthGrade) === 'object' ? JSON.stringify(_this.fromDate.strengthGrade) : _this.fromDate.strengthGrade
            _this.fromDate.performanceGrade = typeof (_this.fromDate.performanceGrade) === 'object' ? JSON.stringify(_this.fromDate.performanceGrade) : _this.fromDate.strengthGrade
            _this.fromDate.meterialRequirements = typeof (_this.fromDate.meterialRequirements) === 'object' ? JSON.stringify(_this.fromDate.meterialRequirements) : _this.fromDate.meterialRequirements
            _this.fromDate.specialRequirements = typeof (_this.fromDate.specialRequirements) === 'object' ? JSON.stringify(_this.fromDate.specialRequirements) : _this.fromDate.specialRequirements
            _this.fromDate.otherRequirements = typeof (_this.fromDate.otherRequirements) === 'object' ? JSON.stringify(_this.fromDate.otherRequirements) : _this.fromDate.otherRequirements
          }
          let allHasData = this.listFilesa.filter((item) => (
            this.oldlistFilesa.some((items) => (item.dataId == items.dataId))
          ))
          let deleteData = this.oldlistFilesa.filter((item) => (
            !allHasData.some((items) => (item.dataId == items.dataId))
          ))
          let addData = this.listFilesa.filter((item) => (
            !allHasData.some((items) => (item.dataId == items.dataId))
          ))

          deleteData = deleteData.map(res => (
            { id: res.dataId, status: 0 }
          ))
          addData = addData.map(res => ({
            fileName: res.fileName,
            filePath: res.path,
            status: 1
          }))
          _this.fromDate.fileList = [...deleteData, ...addData]
          if (_this.fromDate.inspectionResult === 'OK') {
            _this.submitFromDate();
            return
          }
          const copyData = JSON.parse(JSON.stringify(_this.fromDate));
          copyData.fileList = JSON.stringify(copyData.fileList);
          _this.addDialog = false;
          if (_this.active1 === 0) {
            _this.$router.push({ name: 'ThirdCheck1', query: { ...copyData, type: 1 } });
          } else {
            _this.$router.push({ name: 'ThirdCheck2', query: { ...copyData, type: 1 } });
          }
        } else {
          return false;
        }
      });
    },
    sizeChange(value) {
      this.tableParam.pageSize = value;
      this.gettableData();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input,
.el-select {
  width: 240px;
}
.dialoginfo-title {
  display: flex;
  justify-content: space-between;
  padding: 10px 0 20px 0;
  font-weight: bold;
}
.table_link {
  margin: 0 10px;
  color: $bg_color;
}
.download {
  padding: 20px 0 20px 0;
  font-weight: bold;
  display: flex;
  .tl {
    width: 100px;
  }
  .items {
    display: flex;
    flex-wrap: wrap;
    width: 740px;

    .loadItem {
      text-decoration: none;
      white-space: nowrap;
      padding: 0 20px 20px 0;
      font-weight: normal;
      color: $bg_color;
      cursor: pointer;
      opacity: 0.6;
    }
    .loadItem:hover {
      opacity: 1;
    }
  }
}

.table_link:hover {
  opacity: 0.8;
  cursor: pointer;
}
.tabtop {
  width: 381px;
}
</style>
